package com.adgox.tiantianbiting.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.bean.HistoryBean;
import com.adgox.tiantianbiting.home.programDetail.ProgramDetailActivity;
import com.adgox.tiantianbiting.mine.login.LoginActivity;
import com.bumptech.glide.Glide;
import com.rae.swift.session.SessionManager;

import java.util.List;

import javax.microedition.khronos.opengles.GL;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by tang on 2019/1/8.
 */

public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> implements View.OnClickListener {
    private List<HistoryBean> datas;

    public HistoryAdapter(List<HistoryBean> datas) {
        this.datas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_history, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        HistoryBean bean = datas.get(position);
        Glide.with(holder.itemView.getContext()).load(bean.getImageUrl()).into(holder.imgBook);
        holder.tvBookName.setText(bean.getTitle());
        holder.tvBookStatus.setText("("+bean.getStatusStr()+")");
        holder.tvDescribe.setText(bean.getDescribe());
        holder.tvSeriesNum.setText("共 "+bean.getTotalSeason()+"集");
        holder.tvListenNum.setText(bean.getListenNum()+"人正在听");
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void addDatas(List<HistoryBean> list){
        datas.addAll(list);
    }

    @Override
    public void onClick(View view) {
        int position = (int) view.getTag();
        HistoryBean bean = datas.get(position);
        toProgram(view.getContext(),bean.getId(),bean.getTitle());
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.img_book)
        ImageView imgBook;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;
        @BindView(R.id.tv_book_status)
        TextView tvBookStatus;
        @BindView(R.id.tv_describe)
        TextView tvDescribe;
        @BindView(R.id.tv_series_num)
        TextView tvSeriesNum;
        @BindView(R.id.tv_listen_num)
        TextView tvListenNum;
        @BindView(R.id.tv_try)
        TextView tvTry;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    private void toProgram(Context context,int programId, String programName) {
        if (!SessionManager.getDefault().isLogin()) {
            context.startActivity(new Intent(context, LoginActivity.class));
            return;
        }
        Intent intent = new Intent(context, ProgramDetailActivity.class);
        intent.putExtra("programId", programId);
        intent.putExtra("programName", programName);
        context.startActivity(intent);
    }

    public void clear(){
        datas.clear();
        notifyDataSetChanged();
    }

}

